<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>学生管理系统</title>
    <link rel="stylesheet" href="./layui/dist/css/layui.css">
</head>
<style>
</style>

<body>

    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">学生管理系统</div>
            <!-- 头部区域（可配合layui 已有的水平导航） -->
            <ul class="layui-nav layui-layout-left" id="fenban">
                <!-- 移动端显示 -->
                <!-- <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
                    <i class="layui-icon layui-icon-spread-left"></i>
                </li> -->
                <li class="layui-nav-item layui-hide-xs"><a href="">全部</a></li>
                <li class="layui-nav-item layui-hide-xs"><a href="">一班</a></li>
                <li class="layui-nav-item layui-hide-xs"><a href="">二班</a></li>
            </ul>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-md-inline-block" id="mbx">

                </li>
                <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
                    <a href="javascript:;">
                        <i class="layui-icon layui-icon-more-vertical"></i>
                    </a>
                </li>
            </ul>
        </div>
        <!-- 左侧垂直导航栏 -->
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
                <ul class="layui-nav layui-nav-tree" lay-filter="test" id="daohang">
                    <li class="layui-nav-item layui-nav-itemed">
                        <a class="" href="javascript:;">2021</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">会计</a></dd>
                            <dd><a href="javascript:;">计算机</a></dd>
                            <dd><a href="javascript:;">艺术</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a class="" href="javascript:;">2020</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">会计</a></dd>
                            <dd><a href="javascript:;">计算机</a></dd>
                            <dd><a href="javascript:;">艺术</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a class="" href="javascript:;">2019</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">会计</a></dd>
                            <dd><a href="javascript:;">计算机</a></dd>
                            <dd><a href="javascript:;">艺术</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a class="" href="javascript:;">2018</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">会计</a></dd>
                            <dd><a href="javascript:;">计算机</a></dd>
                            <dd><a href="javascript:;">艺术</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a class="" href="javascript:;">2017</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">会计</a></dd>
                            <dd><a href="javascript:;">计算机</a></dd>
                            <dd><a href="javascript:;">艺术</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>
        <script src="./layui/dist/css/layui.css"></script>
        <script>
            //JS 
            layui.use(['element', 'layer', 'util'], function () {
                var element = layui.element
                    , layer = layui.layer
                    , util = layui.util
                    , $ = layui.$;

                //头部事件
                util.event('lay-header-event', {
                    //左侧菜单事件
                    menuLeft: function (othis) {
                        layer.msg('展开左侧菜单的操作', { icon: 0 });
                    }
                    , menuRight: function () {
                        layer.open({
                            type: 1
                            , content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
                            , area: ['260px', '100%']
                            , offset: 'rt' //右上角
                            , anim: 5
                            , shadeClose: true
                        });
                    }
                });
            });
        </script>
        <!-- 左侧垂直导航栏 -->
        <!-- 主体内容 -->
        <div class="layui-body">
            <!-- 内容主体区域 -->
            <div style="padding: 15px;">
                <table class="layui-hide" id="test" lay-filter="test"></table>

                <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
  </div>
</script>
                <script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
                <script src="./layui/dist/css/layui.css" charset="utf-8"></script>
                <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
            </div>
        </div>
        <!-- 主体内容 -->
    </div>
    <script>
        var dds = document.querySelector("#daohang").querySelectorAll("dd");
        var banji = 0
        for (let i = 0; i < dds.length; i++) {
            dds[i].onclick = function () {
                var zhuanye = dds[i].innerText;
                var year = dds[i].parentNode.parentNode.children[0].innerText;
                ajaxGet(year, zhuanye, banji);
                window.sessionStorage["zhuanye"] = `${zhuanye}`;
                window.sessionStorage["year"] = `${year}`;
            }
        }
        var fenbanlis = document.querySelector("#fenban").querySelectorAll("li");
        for (let j = 0; j < fenbanlis.length; j++) {
            fenbanlis[j].onclick = function (e) {
                e.preventDefault();
                banji = j;
                var zhuanye = window.sessionStorage["zhuanye"];
                var year = window.sessionStorage["year"];
                window.sessionStorage["banji"] = `${banji}`;
                ajaxGet(year, zhuanye, banji);
            }
        }
    </script>
    <!-- 封装ajax -->

    <script>
        function ajaxGet(year, zhuanye, banji) {
            layui.use('table', function () {
                var table = layui.table;

                //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
                //详见：https://doc/modules/table.html#totalRow
                table.render({
                    elem: '#test'
                    , url: `/home?year=${year}&zhuanye=${zhuanye}&banji=${banji}`
                    , toolbar: '#toolbarDemo'
                    , title: '用户数据表'
                    , totalRow: true
                    , cols: [[
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'id', width: 80, title: 'ID', sort: true }
                        , { field: 'username', width: 80, title: '用户名' }
                        , { field: 'sex', width: 80, title: '性别' }
                        , { field: 'age', width: 80, title: '年龄', sort: true }
                        , { field: 'period', width: 80, title: '年级', sort: true }
                        , { field: 'spercialty', title: '专业', width: 80 }
                        , { field: 'squad', width: 80, title: '班级', sort: true }
                        , { field: 'phone', width: 80, title: '手机号', width: 150 }
                        , { field: 'native', width: 135, title: '所属地' }
                        , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
                    ]]
                    , page: true
                    , parseData: function (res) {
                        return {
                            "data": res,
                            "code": 0,
                            // "count":res.data,
                        }
                    }
                });
                //工具栏事件
                table.on('toolbar(test)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'getCheckData':
                            var data = checkStatus.data;
                            layer.alert(JSON.stringify(data));
                            break;
                        case 'getCheckLength':
                            var data = checkStatus.data;
                            layer.msg('选中了：' + data.length + ' 个');
                            break;
                        case 'isAll':
                            layer.msg(checkStatus.isAll ? '全选' : '未全选')
                            break;
                    };
                });
                //监听行工具事件
                table.on('tool(test)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    if (obj.event === 'del') {
                        layer.confirm('真的删除行么', function (index) {
                            obj.del();
                            layer.close(index);
                            fetch(`/del?id=${data.id}`, { // 可以获取？后的id page以及他的值，并转换为对象
                                method: "get", //请求类型 默认是get
                            }).then(function (res) {
                                return res.json()
                            })
                        });
                    } else if (obj.event === 'edit') {
                        layer.prompt({
                            formType: 2
                            , value: data.email
                        }, function (value, index) {
                            obj.update({
                                email: value
                            });
                            layer.close(index);
                        });
                    }
                });
            });
        }
        ajaxGet(0);
    </script>
</body>

</html>